package com.ybg.report.column.dao;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.ybg.base.jdbc.BaseDao;
import com.ybg.base.jdbc.BaseMap;
import com.ybg.base.jdbc.util.QvoConditionUtil;
import com.ybg.base.util.Page;
import com.ybg.report.column.domain.ColumnVO;
import com.ybg.report.column.qvo.ColumnQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import com.ybg.base.jdbc.DataBaseConstant;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
/**
 * 
 * 
 * @author Deament
 * @email 591518884@qq.com
 * @date 2018-02-21
 */
@Repository
public class ColumnDaoImpl extends BaseDao implements ColumnDao {
  
	@Autowired
  /**注入你需要的数据库**/
	@Qualifier(DataBaseConstant.JD_REPORT)
	JdbcTemplate jdbcTemplate;
  
	@Override
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	private static String  QUERY_TABLE_COLUMN= "  column.colname, column.colkey, column.colnum, column.gmt_create, column.gmt_modified, column.reportid, column.id";
	private static String  QUERY_TABLE_NAME="report_column  column";
	@Override
	public ColumnVO save(ColumnVO column) throws Exception {
		BaseMap<String, Object> createmap = new BaseMap<String, Object>();
		String id = null;		
		 createmap.put("colname", column.getColname());
		 createmap.put("colkey", column.getColkey());
		 createmap.put("colnum", column.getColnum());
		 createmap.put("gmt_create", column.getGmtCreate());
		 createmap.put("gmt_modified", column.getGmtModified());
		 createmap.put("reportid", column.getReportid());
		 		id = baseCreate(createmap, "report_column", "id");
		column.setId((String) id);
		return column;
	}
	@Override
	public void update(BaseMap<String, Object> updatemap,
			BaseMap<String, Object> WHEREmap) {
		this.baseupdate(updatemap, WHEREmap, "report_column");
	}
	@Override
	public Page list(Page page,  ColumnQuery qvo)throws Exception {
		StringBuilder sql = new StringBuilder();
		sql.append(SELECT).append(QUERY_TABLE_COLUMN).append(FROM)
				.append(QUERY_TABLE_NAME);
		sql.append(getcondition(qvo));
		page.setTotals(queryForInt(sql));
		if (page.getTotals() > 0) {
			page.setResult(getJdbcTemplate().query(page.getPagesql(sql),
					 new BeanPropertyRowMapper<ColumnVO>(ColumnVO.class)));
		} else {
			page.setResult(new ArrayList< ColumnVO>());
		}

		return page;
	}

	private String getcondition( ColumnQuery qvo) throws Exception{
		StringBuilder sql = new StringBuilder();
		sql.append(WHERE).append("1=1");		
      sqlappen(sql, "column.id", qvo.getId());sqlappen(sql, "column.colname", qvo.getColname());sqlappen(sql, "column.colkey", qvo.getColkey());sqlappen(sql, "column.colnum", qvo.getColnum());sqlappen(sql, "column.gmt_create", qvo.getGmtCreate());sqlappen(sql, "column.gmt_modified", qvo.getGmtModified());sqlappen(sql, "column.reportid", qvo.getReportid());		return sql.toString();
	}
	@Override
	public List<ColumnVO> list(ColumnQuery qvo) throws Exception{
		StringBuilder sql = new StringBuilder();
		sql.append(SELECT).append(QUERY_TABLE_COLUMN).append(FROM).append(QUERY_TABLE_NAME);
		sql.append(getcondition(qvo));
		return getJdbcTemplate().query(sql.toString(), new BeanPropertyRowMapper<ColumnVO>(ColumnVO.class));
	}
	
	@Override
	public void remove(BaseMap<String, Object> wheremap) {
		 baseremove(wheremap, "report_column");		
	}
	@Override
	public ColumnVO get(String id){	
		StringBuilder sql = new StringBuilder();
		sql.append(SELECT).append(QUERY_TABLE_COLUMN).append(FROM).append(QUERY_TABLE_NAME);
		sql.append(WHERE).append("1=1");
		sql.append(AND).append("id='"+id+"'");
		List<ColumnVO> list=getJdbcTemplate().query(sql.toString(), new BeanPropertyRowMapper<ColumnVO>(ColumnVO.class));		
		return QvoConditionUtil.checkList(list)?list.get(0):null;
	}
}
